package com.displayCongress;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

/**
 * @author wouter
 */

public class MainActivity extends Activity {

    public static CongressSocket congressSocket;

    protected TextView serverIpView;
    protected TextView usernameView;
    protected TextView passwordView;

    private MainActivity activity;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity = this;
        super.onCreate(savedInstanceState);

        // gets the activity's default ActionBar

        ActionBar actionBar = getActionBar();
        actionBar.setTitle(R.string.mainActivityName);

        setContentView(R.layout.main);

        SharedPreferences settings = getSharedPreferences("serverSettings", 0);

        String serverIp = settings.getString("serverIp", "notSet");

        if(!serverIp.equals("notSet")) {
            ((EditText) findViewById(R.id.serverIpField)).setText(serverIp);
            ((EditText) findViewById(R.id.usernameField)).setText(settings.getString("serverUsername", ""));
            ((EditText) findViewById(R.id.passwordField)).setText(settings.getString("serverPasswd", ""));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // use an inflater to populate the ActionBar with items
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // same as using a normal menu
        switch (item.getItemId()) {
            case R.id.item_save:
                onConnect(null);
                break;
        }

        return true;
    }

    public void makeToast(String message) {
        // with jam obviously
        // I like them with cheese too :)
        Toast.makeText(this.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
    }

    public void onConnect(View view) {
        this.serverIpView = (TextView) findViewById(R.id.serverIpField);
        this.usernameView = (TextView) findViewById(R.id.usernameField);
        this.passwordView = (EditText) findViewById(R.id.passwordField);
        SharedPreferences settings = getSharedPreferences("serverSettings", 0);

        SharedPreferences.Editor editor = settings.edit();
        editor.putString("serverIp", serverIpView.getText().toString());
        editor.putString("serverUsername", usernameView.getText().toString());
        editor.putString("serverPasswd", passwordView.getText().toString());

        // Commit the edits!
        editor.commit();

        makeToast("Connecting now...");
        Thread thread = new Thread(new ConnectionThread(settings));
        Thread thread2 = new Thread(new CheckSocketThread());

        thread.start();
        thread2.start();
    }

    class CheckSocketThread implements Runnable {

        @Override
        public void run() {
            while(true) {
                try {
                    if(congressSocket != null && congressSocket.isLoggedIn())
                    {
                        Looper.prepare();
                        makeToast("Logged IN!");
                        Intent intent = new Intent(activity, SendActivity.class);
                        startActivity(intent);
                        break;
                    }

                    Thread.sleep(150);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
